local amount = tonumber(ARGV[1]);
if (redis.call('exists',KEYS[1])==0) then
    return nil;
end;
local stock = tonumber(redis.call('scard',KEYS[1]));
if (stock == 1) then
    return {-99};
end;
if (stock-1 < amount) then
    amount = stock-1;
end;
local codes = {};
local flag = 0;
for i=1,amount do
    local code = tonumber(redis.call('spop',KEYS[1]));
    if (code == -99) then
        code = tonumber(redis.call('spop',KEYS[1]));
        codes[i] = code;
        flag = 1;
    else
        codes[i] = code;
    end;
end;
if (flag == 1) then
    redis.call('sadd', KEYS[1], -99);
end;
return codes;